package com.ada.tft_sport.fragment;

import android.app.ActionBar.LayoutParams;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.ada.mcu.service.ProgramData;
import com.ada.tft_sport.MainActivity;
import com.ada.tft_sport.R;
import com.ada.tft_sport.utils.BarGraphView;

public class ProgramFragment extends Fragment {

	private BarGraphView mBarGraphView;
	private Handler handler = new Handler();
	MainActivity mainactivity;
	private View programFragment; // 主界面
	private View popwindwow_weight;
	private PopupWindow popup_fitness_program;
	private FragmentManager fManager; // 碎片管理器
	private FragmentTransaction fTransaction; // 获取事务处理对象
	private ImageView burn_sport, climbing_sport, fluctuate_sport,
			interval_sport, lost_weight_sport, mountion_sport;

	private  ProgramData mProgramData = ProgramData.getInstance();  
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		// 每次切换此页面时，不需要重新创建Fragment对象
		setRetainInstance(true);
	}

	@Override
	public View onCreateView(LayoutInflater inflater,
			@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

		// 实例化布局文件
		programFragment = inflater.inflate(R.layout.fragment_program,
				container, false);
		popwindwow_weight = inflater.inflate(R.layout.pop_fintess_program,
				container, false);
		fManager = getActivity().getSupportFragmentManager(); // 获得碎片管理器
		fTransaction = fManager.beginTransaction(); // 获取事务处理对象

		initProgram();
		onClickListener();
		
        Log.i("999999999999", "9999999999"+mProgramData.getAge());
		return programFragment; // 返回视图对象

	}

	
	    
	
	private void showPopFitness(final int  programIncline, final int  programSpeed) {
		// TODO Auto-generated method stub
		if (null == popup_fitness_program || !popup_fitness_program.isShowing()) {

			popup_fitness_program = new PopupWindow(popwindwow_weight,
					LayoutParams.WRAP_CONTENT, 530);
			popup_fitness_program.showAtLocation(
					programFragment.findViewById(R.id.LinearLayout2),
					Gravity.CENTER  , 18, -26);
			popup_fitness_program.setBackgroundDrawable(new BitmapDrawable());
			popup_fitness_program.setOutsideTouchable(true);

			mBarGraphView = (BarGraphView) popwindwow_weight
					.findViewById(R.id.custom_view);
			TextView program_over=(TextView)popwindwow_weight.findViewById(R.id.program_over);
			mBarGraphView.setAxisX(1200, 21);
			mBarGraphView.setAxisY(200, 20);

			program_over.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View arg0) {
					// TODO Auto-generated method stub
					popup_fitness_program.dismiss();
				}
			});
			
			final int[][] programInclineArray = {
					{ 0, 0, 0, 2, 2, 2, 3, 3, 3, 4, 4, 4, 4, 0, 0, 0, 0, 2, 2,
							2 },
					{ 0, 0, 1, 2, 3, 4, 4, 3, 2, 1, 1, 2, 3, 4, 4, 3, 2, 1, 0,
							0 },
					{ 0, 0, 0, 4, 4, 4, 1, 1, 1, 4, 4, 4, 1, 1, 1, 4, 4, 4, 0,
							0 },
					{ 0, 0, 2, 4, 6, 6, 4, 2, 4, 6, 6, 4, 2, 4, 6, 6, 4, 2, 0,
							0 },
					{ 0, 0, 0, 6, 6, 2, 2, 6, 6, 2, 2, 6, 6, 2, 2, 6, 6, 0, 0,
							0 },
					{ 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 4, 4, 3, 3, 2, 2, 1, 1, 0,
							0 },
					{ 0, 0, 0, 1, 2, 3, 4, 5, 5, 5, 5, 5, 5, 4, 3, 2, 1, 0, 0,
							0 } };

			final int[][] programSpeedArray = {
					{ 30, 30, 30, 50, 50, 50, 70, 70, 70, 90, 90, 90, 90, 30,
							30, 30, 30, 50, 50, 50 },
					{ 30, 30, 40, 50, 60, 70, 70, 60, 50, 40, 40, 50, 60, 70,
							70, 60, 50, 40, 30, 30 },
					{ 30, 30, 30, 70, 70, 70, 40, 40, 40, 70, 70, 70, 40, 40,
							40, 70, 70, 70, 30, 30 },
					{ 30, 30, 50, 70, 90, 90, 70, 50, 70, 90, 90, 70, 50, 70,
							90, 90, 70, 50, 30, 30 },
					{ 30, 30, 30, 90, 90, 50, 50, 90, 90, 50, 50, 90, 90, 50,
							50, 90, 90, 30, 30, 30 },
					{ 30, 30, 40, 40, 50, 50, 60, 60, 70, 70, 70, 70, 60, 60,
							50, 50, 40, 40, 30, 30 },
					{ 30, 30, 30, 40, 50, 60, 70, 80, 80, 80, 80, 80, 80, 70,
							60, 50, 40, 30, 30, 30 } };

			mBarGraphView.setColumnInfo(programSpeedArray[programSpeed],
					programInclineArray[programIncline]);
//			handler.post(new Runnable() {
//				@Override
//				public void run() {
//					// TODO Auto-generated method stub
//					int number = (int) (Math.random() * 7);
//					mBarGraphView.setColumnInfo(programSpeedArray[programSpeed],
//							programInclineArray[programIncline]);
//					handler.postDelayed(this, 1000);
//					mBarGraphView.invalidate();
//				}
//			});
		}
	}

	private void onClickListener() {
		// TODO Auto-generated method stub
		burn_sport.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				showPopFitness(1,1);
				mProgramData.setProgramIndex(0);
				Log.i("777777","7777777mProgramData.getProgramIndex"+mProgramData.getProgramIndex());
				
			}
		});
		climbing_sport.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				showPopFitness(2,2);
				mProgramData.setProgramIndex(1);
				Log.i("777777", "7777777mProgramData.getProgramIndex" + mProgramData.getProgramIndex());
			}
		});
		
		fluctuate_sport.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				showPopFitness(3,3);
				mProgramData.setProgramIndex(2);
				Log.i("777777", "7777777mProgramData.getProgramIndex" + mProgramData.getProgramIndex());
			}
		});
		interval_sport.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				showPopFitness(4,4);
				mProgramData.setProgramIndex(3);
				Log.i("777777", "7777777mProgramData.getProgramIndex" + mProgramData.getProgramIndex());
			}
		});
		lost_weight_sport.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				showPopFitness(5,5);
				mProgramData.setProgramIndex(4);
				Log.i("777777", "7777777mProgramData.getProgramIndex" + mProgramData.getProgramIndex());
			}
		});
		mountion_sport.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				showPopFitness(6,6);
				mProgramData.setProgramIndex(5);
				Log.i("777777", "7777777mProgramData.getProgramIndex" + mProgramData.getProgramIndex());
			}
		});
	}

	private void initProgram() {
		// TODO Auto-generated method stub
		burn_sport = (ImageView) programFragment.findViewById(R.id.burn_sport);
		climbing_sport = (ImageView) programFragment
				.findViewById(R.id.climbing_sport);
		fluctuate_sport = (ImageView) programFragment
				.findViewById(R.id.fluctuate_sport);
		interval_sport = (ImageView) programFragment
				.findViewById(R.id.interval_sport);
		lost_weight_sport = (ImageView) programFragment
				.findViewById(R.id.lost_weight_sport);
		mountion_sport = (ImageView) programFragment
				.findViewById(R.id.mountion_sport);
	}

}
